﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace SerializeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Card[] orginalCards = new Card[13];
            orginalCards[0] = new Card(CardType.Heart, CardNumberType.A);
            orginalCards[1] = new Card(CardType.Heart, CardNumberType.Eight);
            orginalCards[2] = new Card(CardType.Heart, CardNumberType.Five);
            orginalCards[3] = new Card(CardType.Heart, CardNumberType.Four);
            orginalCards[4] = new Card(CardType.Heart, CardNumberType.Jack);
            orginalCards[5] = new Card(CardType.Heart, CardNumberType.King);
            orginalCards[6] = new Card(CardType.Heart, CardNumberType.Nine);
            orginalCards[7] = new Card(CardType.Heart, CardNumberType.Queen);
            orginalCards[8] = new Card(CardType.Heart, CardNumberType.Seven);
            orginalCards[9] = new Card(CardType.Heart, CardNumberType.Six);
            orginalCards[10] = new Card(CardType.Heart, CardNumberType.Ten);
            orginalCards[11] = new Card(CardType.Heart, CardNumberType.Three);
            orginalCards[12] = new Card(CardType.Heart, CardNumberType.Two);
            //Person p = new Person() { Name = "mks", Age = 33 };
            //Card c = new Card(CardType.Club, CardNumberType.Eight);
            byte[] bytes = SerializeHelper.ToBytes<Card[]>(orginalCards);
            

            Console.WriteLine(Encoding.Default.GetString(bytes));

            Card[] cards = SerializeHelper.FromBytes<Card[]>(bytes);
            //Console.WriteLine(c.CardType+"  "+c.CardNumberType);
            Console.WriteLine(bytes.Length);
            for (int i = 0; i < cards.Length; i++)
            {
                Console.WriteLine(cards[i].CardType + "  " + cards[i].CardNumberType);
            }
                Console.ReadLine();
        }
    }

    [Serializable]
    public class Person
    {
        private string name;
        private int age;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }

    public class SerializeHelper
    {
        private static BinaryFormatter bf;
        static SerializeHelper()
        {
            bf = new BinaryFormatter();
        }

        public static byte[] ToBytes<T>(T obj)
        {
            MemoryStream ms = new MemoryStream();
            
            bf.Serialize(ms, obj);
            return ms.GetBuffer(); 
        }

        public static T FromBytes<T>(byte[] bytes)
        {
            MemoryStream ms = new MemoryStream(bytes);
            return (T)bf.Deserialize(ms);
        }
    }

    

    public enum CardType
    {
        Heart,
        Spade,
        Club,
        Diamond
    }

    public enum CardNumberType
    {
        A,
        Two,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King
    }
    [Serializable]
    public class Card
    {
        private CardType cardType;
        private CardNumberType cardNumberType;

        public CardType CardType
        {
            set { cardType = value; }
            get { return cardType; }
        }

        public CardNumberType CardNumberType
        {
            set { cardNumberType = value; }
            get { return cardNumberType; }
        }

        public Card(CardType ct, CardNumberType cnt)
        {
            this.cardType = ct;
            this.cardNumberType = cnt;
        }
    }
}
